package com.CusterRelSys.control;

import com.CusterRelSys.entity.CstLinkman;
import com.CusterRelSys.service.CstLinkmanServiceZYG;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

/**
 * Created by 周钰贵 on 2017/12/1 0001.
 */
@Controller
@RequestMapping("/CstLinkman")
public class CstLinkmanController {

    @Autowired
    private CstLinkmanServiceZYG cstLinkmanServiceZYG;

    @RequestMapping(value = "/selectCstLinkmanForNo",method = RequestMethod.GET)
    public @ResponseBody List<CstLinkman> selectCstLinkmanForNo(HttpServletRequest request){
        String lkmCustNo = request.getParameter("lkmCustNo");
        System.out.println(lkmCustNo);
        CstLinkman cstLinkman=new CstLinkman(lkmCustNo);
        List<CstLinkman> cstLinkmen = cstLinkmanServiceZYG.selectCstLinkmanForNo(cstLinkman);
        for (CstLinkman linkman : cstLinkmen) {
            System.out.println(linkman);
        }
        return cstLinkmen;
    }

    @RequestMapping(value = "/insertCstLinkman",method = RequestMethod.GET)
    public @ResponseBody int insertCstLinkman(HttpServletRequest request){
        String lkmCustNo = "";
        Random random = new Random();
        for (int i = 0; i <= 10; i++) {
            boolean b = random.nextBoolean();
            if (b) { // 字符串
                // int choice = random.nextBoolean() ? 65 : 97; 取得65大写字母还是97小写字母
                lkmCustNo += (char) (65 + random.nextInt(26));// 取得大写字母
            } else { // 数字
                lkmCustNo += String.valueOf(random.nextInt(10));
            }
        }
        String lkmName = request.getParameter("lkmName");
        String lkmSex = request.getParameter("lkmSex");
        String lkmPostion = request.getParameter("lkmPostion");
        String lkmTel = request.getParameter("lkmTel");
        String lkmMobile = request.getParameter("lkmMobile");
        String lkmMemo = request.getParameter("lkmMemo");
        CstLinkman cstLinkman=new CstLinkman(lkmCustNo,lkmName,lkmSex,lkmPostion,lkmTel,lkmMobile,lkmMemo);
        System.out.println(cstLinkman);
        int i = cstLinkmanServiceZYG.insertCstLinkman(cstLinkman);
        return i;
    }
    @RequestMapping(value = "/deleteForName",method = RequestMethod.GET)
    public @ResponseBody int deleteForName(HttpServletRequest request){
        String lkmName = request.getParameter("lkmName");
        int i = cstLinkmanServiceZYG.deleteForName(lkmName);
        return i;
    }

    @RequestMapping(value = "/selectCstLinkmanForId",method = RequestMethod.GET)
    public @ResponseBody CstLinkman selectCstLinkmanForId(HttpServletRequest request){
        int lkmId = Integer.parseInt(request.getParameter("lkmId"));
        CstLinkman cstLinkmanForId = cstLinkmanServiceZYG.selectCstLinkmanForId(lkmId);
        return cstLinkmanForId;
    }
    @RequestMapping(value = "/updateCstLinkman",method = RequestMethod.GET)
    public @ResponseBody int updateCstLinkman(HttpServletRequest request){
        String lkmName = request.getParameter("lkmName");
        String lkmSex = request.getParameter("lkmSex");
        String lkmPostion = request.getParameter("lkmPostion");
        String lkmTel = request.getParameter("lkmTel");
        String lkmMobile = request.getParameter("lkmMobile");
        String lkmMemo = request.getParameter("lkmMemo");
        CstLinkman cstLinkman=new CstLinkman(lkmName,lkmSex,lkmPostion,lkmTel,lkmMobile,lkmMemo);
        int i = cstLinkmanServiceZYG.updateCstLinkman(cstLinkman);
        return i;
    }
}
